package sisi.captains;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * CaptainController.java - Controller class for the <i>Captains database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 03/12/2013
 * 
 */
public class CaptainController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Captain[] get(String cerca) {
		List<Captain> lista = getList(cerca);
			return lista.toArray(new Captain[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Captain> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.contactname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Captain as c " + cWhere 
					+ " order by c.contactname ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Captain> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	public Captain getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Captain captainx = em.find(Captain.class, id);
			return captainx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Captain add(Captain captain) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(captain);
			em.getTransaction().commit();
			captain = refresh(captain);
		} finally {
			em.close();
			return captain;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Captain captain) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Captain captainx = em.find(Captain.class, captain.getId());
			captainx.setAddress(captain.getAddress());
			captainx.setId(captain.getId());
			captainx.setCountry(captain.getCountry());
			captainx.setPosition(captain.getPosition());
			captainx.setNote(captain.getNote());
			captainx.setEmail(captain.getEmail());
			captainx.setFax(captain.getFax());
			captainx.setBirthdt(captain.getBirthdt());
			captainx.setBoat(captain.getBoat());
			captainx.setCity(captain.getCity());
			captainx.setContactname(captain.getContactname());
			captainx.setDtup(captain.getDtup());
			captainx.setGsm(captain.getGsm());
			captainx.setLongdiscode(captain.getLongdiscode());
			captainx.setRegion(captain.getRegion());
			captainx.setTelnum(captain.getTelnum());
			captainx.setZip(captain.getZip());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Captain refresh(Captain captain) {

		Captain captainx = captain;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			captainx = em.find(Captain.class, captain.getId());
			em.flush();
			em.refresh(captainx);
		} finally {
			em.close();
			return captainx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Captain captain) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Captain captainx = em.find(Captain.class, captain.getId());
			em.remove(captainx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
